<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-5-18
 * Time: 上午9:59
 */

class comment extends adminBase{
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("评价管理");
        $this->cms->setControlFile("admin/tpl/comment/comment_control.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(7,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($page=1,$goodsName,$phone,$real_name){
        $this->cms->setActionTitle("所有评价");
        $user=UserMode::init();
        $goods=GoodsMode::init();
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $phone=isset($phone)?$phone:null;
        if(isset($phone)&&!empty($phone)){
            $user_id=$user->getIdByUsername($phone);
        }else{
            $user_id=null;
        }
        $real_name=isset($real_name)?$real_name:null;
        if(isset($real_name)&&!empty($real_name)){
            $user_check=$user->getInfoByRealName($real_name);
            if(isset($user_check)&&!empty($user_check)){
                $user_id=$user_check['user_id'];
            }else{
                $user_id=null;
            }
        }
        if(isset($goodsName)){
            list($goods_ids,$goods_ids_total)=$goods->getList(null,null,null,$goodsName,null,false,100,0);
        }else{
            $goods_ids = null;
        }
        $goods_comment=CommentModule::init("goods_comment",'user_id','gopen_id');
        list($r['goods_comment'],$goods_comment_total)=$goods_comment->getList($user_id,$goods_ids,$countOfPage,$offset);
        $r['goods_comment']=is_array($r['goods_comment'])?$r['goods_comment']:array();
        foreach($r['goods_comment'] as &$v){
            $user_info=$user->getDetail($v['user_id']);
            $v['user_id']=$user_info['real_name'];
            $goods_info=$goods->getDetailInfo($v['gopen_id']);
            if(isset($goods_info['name']))
            $v['gopen_id']=$goods_info['name'];
        }
        $r['pages']=new Page($page,$goods_comment_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $this->cms->tableScene($r,"admin/tpl/comment/comment.php");
    }

    function commentDetailTask($id){
        $comment=CommentModule::init('goods_comment');
        $r=$comment->getDetail($id);
        $goods=GoodsMode::init();
        $r['goods_info']=$goods->getDetailInfo($r['gopen_id']);
        $r['goods_info']=is_array($r['goods_info'])?$r['goods_info']:array();
        $shop=ShopMode::init();
        $r['shop_name']=$shop->getDetail($r['goods_info']['shop_id']);
        $user=UserMode::init();
        $r['user_name']=$user->getDetailWithoutJoin($r['user_id']);
        $this->cms->formScene($r,"admin/tpl/comment/commentDetail.php");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(7,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function commentDeleteTask($id){
        $comment=CommentModule::init('goods_comment');
        if($comment->delete($id)){
            $r['message']="删除成功";
            $r['url']=WebRouter::init()->getAction("index");
        }else{
            $r['message']="删除失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function serviceTask($page=1,$serviceId){
        $this->cms->setActionTitle("所有服务站评价");
        $user=UserMode::init();
        $order=OrderMode::init();
        $service=ServiceMode::init();
        $r['serviceId']=$serviceId;
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
//        $user_id=$user->getIdByUsername($phone);
        $service_comment=CommentModule::init("service_comment",'order_id','service_id');
        list($r['service_comment'],$service_comment_total)=$service_comment->getList(null,$serviceId,$countOfPage,$offset);
        $r['service_comment']=is_array($r['service_comment'])?$r['service_comment']:array();
        foreach($r['service_comment'] as &$v){
            $order_info=$order->getDetail($v['order_id']);
            $user_info=$user->getDetail($order_info['order']['user_id']);
            $v['user_id']=$user_info['real_name'];
            $service_info=$service->getDetail($v['service_id']);
            $v['service_id']=$service_info['service_name'];
        }
        list($r['service_info'],$service_total)=$service->getList(2,100,0);
        usort($r['service_info'],function($a,$b){
            return ord(substr(iconv('utf-8', 'gbk', $a['service_name']), 0, 1))-
            ord(substr(iconv('utf-8', 'gbk', $b['service_name']), 0, 1));
        });
        $r['pages']=new Page($page,$service_comment_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $this->cms->tableScene($r,"admin/tpl/comment/comment_service.php");
    }

    function serviceDetailTask($id){
        $comment=CommentModule::init('service_comment');
        $r=$comment->getDetail($id);
        $service=ServiceMode::init();
        $r['service_info']=$service->getDetail($r['service_id']);
        $this->cms->formScene($r,"admin/tpl/comment/serviceDetail.php");
    }

    function serviceDeleteTask($id){
        $comment=CommentModule::init('service_comment');
        if($comment->delete($id)){
            $r['message']="删除成功";
            $r['url']=WebRouter::init()->getAction("service");
        }else{
            $r['message']="删除失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
} 